package cn.jhc.teachingquality.task;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;

import com.android.volley.toolbox.RequestFuture;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

import cn.jhc.teachingquality.Global;
import cn.jhc.teachingquality.activity.MainActivity;
import cn.jhc.teachingquality.net.RequestQueueFactory;
import cn.jhc.teachingquality.net.teaching.CheckTeachingLogRequest;
import cn.jhc.teachingquality.net.teaching.FetchStudentsRequest;
import cn.jhc.teachingquality.net.teaching.ListUncheckedTeachingLogsRequest;

/**
 * 审核所有教学日志。
 * Created by luyanfei on 16-4-16.
 */
public class VerifyTeachingLogTask extends AsyncTask<Void,Integer,Integer> {

    private MainActivity mainActivity;

    public VerifyTeachingLogTask(MainActivity activity){
        this.mainActivity = activity;
    }

    @Override
    protected Integer doInBackground(Void... params) {
        RequestFuture<List<String>> future = RequestFuture.newFuture();
        ListUncheckedTeachingLogsRequest listRequest =
                new ListUncheckedTeachingLogsRequest(future);
        RequestQueueFactory.getRequestQueue(mainActivity).add(listRequest);
        List<String> hiddenIds;
        try {
            hiddenIds = future.get();
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
            return 0;
        }
        Log.d(Global.LOG_TAG, "Found " + hiddenIds.size() + " unchecked teaching logs.");
        for(String id : hiddenIds){
            checkTeachingLog(id, fetchStudentIds(id));
        }
        return hiddenIds.size();
    }

    private void checkTeachingLog(String courseId, List<String> studentIds){
        CheckTeachingLogRequest checkRequest = new CheckTeachingLogRequest(courseId, studentIds);
        RequestQueueFactory.getRequestQueue(mainActivity).add(checkRequest);
    }

    private List<String> fetchStudentIds(String courseId) {
        RequestFuture<JSONArray> future = RequestFuture.newFuture();
        FetchStudentsRequest fetchRequest = new FetchStudentsRequest(courseId,future);
        RequestQueueFactory.getRequestQueue(mainActivity).add(fetchRequest);
        JSONArray jsonArray;
        try {
            jsonArray = future.get();
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
//        Log.d(Global.LOG_TAG, "The students json array has " + jsonArray.length() + " entries.");
        List<String> studentIds = new ArrayList<>(jsonArray.length());
        try {
            for(int i=0; i<jsonArray.length(); i++){
                JSONArray innerArray = jsonArray.getJSONArray(i);
                //需要提交的学生id为内嵌数据的第0个元素。
                String userid = innerArray.getString(0);
                studentIds.add(userid);
            }
        } catch (JSONException e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
//        Log.d(Global.LOG_TAG, "The students id list has " + studentIds.size() + " student ids.");
        return studentIds;
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
    }

    @Override
    protected void onPostExecute(Integer result) {
        Toast.makeText(mainActivity,
                "为您自动审核了" + result + "条教学日志。",Toast.LENGTH_LONG).show();
    }

}
